package com.huatai.exchange.bootup.controller;

import com.faneui.common.bean.FaneuiBaseResp;
import com.faneui.common.utils.CloseUtils;
import com.faneui.common.utils.IOUtils;
import com.faneui.integration.smb.SmbFileUtils;
import com.huatai.exchange.bootup.task.BackupFileTask;
import com.huatai.exchange.integration.base.service.FileOperateService;
import com.huatai.exchange.service.ExcChannelInfoService;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

@RestController
@RequestMapping(value = "test")
public class TestController extends AbstractRestController {

    private static final Logger logger = LoggerFactory.getLogger(AbstractRestController.class);

    @Autowired
    private ExcChannelInfoService excChannelInfoService;

    @Autowired
    private FileOperateService fileOperateService;

    @RequestMapping(value = "/1", method = RequestMethod.GET)
    public FaneuiBaseResp test1(@RequestParam String exchangeInfoId
            , @RequestParam String occurDate) throws Exception {
        FaneuiBaseResp resp = new FaneuiBaseResp();
        try {
            excChannelInfoService.executeExchangeDataFile(exchangeInfoId, occurDate);
        } catch (Exception ex) {
            resp.setSuccess(false);
            resp.setMsg(ex.getMessage());
        }
        return resp;
    }

    /**
     *  测试文件读取 速度
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/2", method = RequestMethod.GET)
    public FaneuiBaseResp test2() throws Exception {
        FaneuiBaseResp resp = new FaneuiBaseResp();
        InputStream is = null;
        OutputStream os = null;
        File f = new File("/root/app/file/tmp/SJSGB1210.DBF");
        try {
            String smbFileUrl = SmbFileUtils.H_url+"DCOM/20181210/SJSGB1210.DBF";
            SmbFile smbFile = new SmbFile(smbFileUrl,SmbFileUtils.createAuthHdisk()) ;
            smbFile.connect();
            is = new SmbFileInputStream(smbFile) ;
            os = new FileOutputStream(f) ;
            IOUtils.download(os,is);
            logger.info("下载完成，开始上传");
            String filePath = fileOperateService.uploadFile(f);
            logger.info(filePath);
            resp.setMsg(filePath);
        } catch (Exception ex) {
            resp.setSuccess(false);
            resp.setMsg(ex.getMessage());
        }finally {
            CloseUtils.safeClose(os,is);
            f.deleteOnExit();
        }
        return resp;
    }

}
